<?php

class edit_admins
{
	function edit_admins()
	{
		global $skin_dir, $db, $lang;
		
		if(isset($_POST['remAdmin']) && isset($_POST['remAdminID']))
		{
			$remQ = 'delete from '. ADMIN_TABLE .' where a_id ='. $_POST['remAdminID'];
			$result = $db->sql_query($remQ);
			$selQ = 'select u.user_id, u.username, pf_drivername as user_drivername 
					 FROM ' . USERS_TABLE . ' u, '. DRIVERNAME_TABLE . ' d ' .
					' where u.user_id = p.user_id AND u.user_id = ' .  $_POST['remAdminID'];
			$result = $db->sql_query($selQ);
			if($row = $db->sql_fetchrow($result)) {
				$mes = new addMessage(str_replace('{entry}', $row['user_drivername'] . ' (' . $row['username'] . ')', $lang['AdminRemoved']),
										$lang['AdminRemovedTitle']);
				$mes->render();
			}
			else
			{
				$mes = new addMessage($lang['DatabaseError'], $lang['DatabaseErrorTitle']);
				$mes->render();
			}
		}
		else if(isset($_POST['addAdmin']) && isset($_POST['addAdminID']))
		{
			$insQ = 'insert into '. ADMIN_TABLE .' values ('. $_POST['addAdminID'] .')';
			$result = $db->sql_query($insQ);
			$selQ = 'select u.user_id, u.username, pf_drivername as user_drivername 
					 FROM ' . USERS_TABLE . ' u, '. DRIVERNAME_TABLE . ' d ' .
					' where u.user_id = d.user_id AND u.user_id = ' . $_POST['addAdminID'];
			$result = $db->sql_query($selQ);
			if($row = $db->sql_fetchrow($result)) {
				$mes = new addMessage(str_replace('{entry}', $row['user_drivername'] . ' (' . $row['username'] . ')', $lang['AdminAdded']),
										$lang['AdminAddedTitle']);
				$mes->render();
			}
		}
	}
	
	function render()
	{
		global $skin_dir, $db, $lang;
		
		$currentAdmins = '';
		$nonAdmins = '';
		
		//two sql statements
		$q_none = 'SELECT u.user_id, u.username, pf_drivername as user_drivername 
					FROM ' . USERS_TABLE . ' u, '. USERSGROUPS_TABLE . ' g, ' . DRIVERNAME_TABLE . ' d ' .
				 	' WHERE u.user_id = d.user_id AND g.user_id = u.user_id AND g.group_id = 3 AND u.user_id NOT IN ' .
					'(SELECT a.a_id FROM ' . ADMIN_TABLE . ' a) ORDER BY pf_drivername ASC';
		$q_current = "SELECT u.user_id, u.username, pf_drivername as user_drivername  
					FROM " . ADMIN_TABLE . " a, " . USERS_TABLE . ' u, ' . DRIVERNAME_TABLE . ' d ' .
					" WHERE u.user_id = d.user_id AND a.a_id = u.user_id ORDER BY pf_drivername ASC";
	
		$result = $db->sql_query($q_none);
		while($row = $db->sql_fetchrow($result)) {
			$nonAdmins = $nonAdmins . '<option value="' . $row['user_id'] . '">' . $row['user_drivername'] . 
										' (' . $row['username'] . ')</option>';
		}
		
		$result = $db->sql_query($q_current);
		while($row = $db->sql_fetchrow($result)) {
			$currentAdmins = $currentAdmins . '<option value="' . $row['user_id'] . '">' . $row['user_drivername'] . 
										' (' . $row['username'] . ')</option>';
		}
		
		include $skin_dir . 'admin/admins.tpl';
	}

}

?>